package com.android.locationapi.maps;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.google.android.maps.MapActivity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class deleteFriend extends Activity{
	
	ProgressDialog myProgressDialog = null;
	
	private EditText name;
	private EditText email;
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		
		setContentView(R.layout.deletefriend);
        final Context ctx = this.getApplicationContext();
		name = (EditText) findViewById(R.id.friendNameTextBox);
		
		Button saveButton = (Button) findViewById(R.id.saveButton);
	    saveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
	    		myProgressDialog = ProgressDialog.show(deleteFriend.this,"Please wait...", "Creating new user...", true);
	    		
                final String _name = name.getText().toString();
                Log.v("ramya : value for creating the username :",creden.usernme);
                Log.v("ramya : value for creating the username :",_name);

                
     	        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
     	        nameValuePairs.add(new BasicNameValuePair("username",creden.usernme));
       	        nameValuePairs.add(new BasicNameValuePair("name",_name));
       	        
  	            InputStream is = null;
      	        String result = "";

     	          try{
   	                  HttpClient httpclient = new DefaultHttpClient();
   	                  HttpPost httppost = new HttpPost(serveripaddress.servipaddr+"delete_friend.php");
   	                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
   	                  Log.v("Ramya", "AddFriends I am done 1!!!");
   	                  HttpResponse response = httpclient.execute(httppost);
   	                  Log.v("Ramya", "AddFriends I am done 2!!!");
   	                  HttpEntity entity = response.getEntity();
   	                  Log.v("Ramya", "AddFriends here now");
   	                  is = entity.getContent();
   	                  Log.v("Ramya: Returned from the server 0 : ", is.toString());

   	          }catch(Exception e){
   	                  Log.v("log_tag", "Error in http connection "+e.toString());
   	          }
   	          //convert response to string
   	          try{
   	                  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
   	                  StringBuilder sb = new StringBuilder();
   	                  String line = null;
   	                  while ((line = reader.readLine()) != null) {
   	                          sb.append(line);
   	                  }
   	                  is.close();
   	           
   	                  result=sb.toString();
   	                  Log.v("Ramya: Returned from the server : ", result);
   	                  
   	                  if ( result.equals("DELETED_FRIEND"))
   	                	  
   	                  {
   	                	 Toast.makeText(ctx,"Successfully deleted the friend", 9000).show();
   	                	 Intent explicitIntent5= new Intent(deleteFriend.this,ShowMap.class);
                         
                         startActivity(explicitIntent5);
   	                  }
   	                  else  if ( result.equals("DID_NOT_ENTRY"))
   	                  {
   	                	 Toast.makeText(ctx,"Did not find the friend in your friend list", 6000).show();
   	                	Intent explicitIntent6= new Intent(deleteFriend.this,deleteFriend.class);
   	                  }
   	                  
   	                  else if (result.equals("FAILED_TO_DELETE"))
   	                  {
   	                	 Toast.makeText(ctx,"Error!!! :: Failed to delete friend from list", 6000).show();
   	                	Intent explicitIntent6= new Intent(deleteFriend.this,deleteFriend.class);
   	                	  
   	                  }
   	          }catch(Exception e){
   	                  Log.v("log_tag", "Error converting result "+e.toString());
   	          }
             myProgressDialog.dismiss();
				// TODO Auto-generated method stub
				//Toast toastName = Toast.makeText(AddFriend.this, name.getText().toString(), Toast.LENGTH_SHORT);
				//toastName.show();
				//Toast toastEmail = Toast.makeText(AddFriend.this, email.getText().toString(), Toast.LENGTH_SHORT);
				//toastEmail.show();
			}
		});
	    
	    Button cancelButton = (Button) findViewById(R.id.cancelButton);
	    cancelButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent myIntent = new Intent(deleteFriend.this, ShowMap.class);
	        	deleteFriend.this.startActivity(myIntent);
			}
		});
		
		
	}
}
